package com.example.aslongastoday.util;

import static android.content.Context.MODE_PRIVATE;

import android.app.Activity;
import android.content.SharedPreferences;

import com.example.aslongastoday.MainActivity;
import com.example.aslongastoday.R;
import com.example.aslongastoday.bean.BeanFactory;
import com.example.aslongastoday.domain.BackLogGroup;
import com.example.aslongastoday.domain.Memory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class CloudUtil {
    public static Gson gson = BeanFactory.makeGsonBean();
    public static String REMOTE_URI = "https://qianicloud.cn/api/";

    public static String getLoginUserInfo() {
        MainActivity mainActivity = (MainActivity)BeanFactory.getBean("mainActivity");
        SharedPreferences sharedPreferences = mainActivity.getSharedPreferences("loginInfo", MODE_PRIVATE);
        String userName = sharedPreferences.getString("user", "--");
        return userName;
    }

    public static void PushBackLog(List<BackLogGroup> backLogGroupList) throws ExecutionException, InterruptedException {
        String userName = getLoginUserInfo();
        Future<JsonElement> post = OkHttpUtil.post(REMOTE_URI+"back_log/add?userName=" + userName,
                gson.toJson(backLogGroupList));
        JsonElement jsonElement = post.get();
    }



    public static List<BackLogGroup>  PullBackLog() throws InterruptedException, ExecutionException {
        String userName = getLoginUserInfo();
        Future<JsonElement> future = OkHttpUtil.get(REMOTE_URI+"back_log/get?userName=" + userName);

        JsonElement jsonElement = future.get();
        Type type = new TypeToken<List<BackLogGroup>>() {}.getType();

        Object o = gson.fromJson(jsonElement, type);

        return (List<BackLogGroup>) o;
    }

    public static void PushMemory(String userName, List<Memory> memoryList) throws ExecutionException, InterruptedException {
        userName = getLoginUserInfo();
        Future<JsonElement> post = OkHttpUtil.post(REMOTE_URI+"back_log/add?userName=" + userName,
                gson.toJson(memoryList));
        JsonElement jsonElement = post.get();
    }

    public static List<Memory> PullMemory(String userName) throws ExecutionException, InterruptedException {
        userName = getLoginUserInfo();
        Future<JsonElement> future = OkHttpUtil.get(REMOTE_URI + "memory/get?userName=" + userName);

        JsonElement jsonElement = future.get();
        Type type = new TypeToken<List<Memory>>(){}.getType();

        Object o = gson.fromJson(jsonElement, type);
        return (List<Memory>) o;
    }

    public static void Register(String userName,String password) throws ExecutionException, InterruptedException {
        HashMap<String, String> map = new HashMap<>();
        map.put("userName",userName);
        map.put("userPassword",password);
        String s = gson.toJson(map);
        OkHttpUtil.post(REMOTE_URI+"user/register",s).get();
    }

    public static boolean Login(String userName,String password) throws InterruptedException, ExecutionException {
        HashMap<String, String> map = new HashMap<>();
        map.put("userName",userName);
        map.put("userPassword",password);
        JsonElement jsonElement = OkHttpUtil.login(REMOTE_URI + "user/login", gson.toJson(map)).get();
        return jsonElement.toString().equals("200");
    }
}
